﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeaven.Admin.Common;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.ComMethod;
using System.Text;

namespace FortuneHeaven.Admin.Pages.GameManager
{
    public partial class CardGenerate : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get { return "F53"; }
        }

        protected string ShowCode(string code)
        {
            return FortuneHeavenCommon.ComMethod.CommonEncrypt.Decode(code).Substring(0, 9) + "**********";
        }

        protected string ShowCodeFull(string code)
        {
            return FortuneHeavenCommon.ComMethod.CommonEncrypt.Decode(code);
        }

        IList<NetBarInfo> NetBarList = null;//netBarBO.GetNetBarInforsNameID()

        protected void LoadNetBarList()
        {
            if (NetBarList != null)
                return;
            NetBarBO netBarBO = new NetBarBO();
                NetBarList = netBarBO.GetNetBarInforsNameID();
        }

        protected void LoadNetBar()
        {
            LoadNetBarList();
            if (NetBarList != null)
            {
                this.dpNetBar.Items.Clear();
                this.dpNetBar.DataSource = NetBarList;
                this.dpNetBar.DataBind();
                this.drpNetBar2.Items.Clear();
                this.drpNetBar2.DataSource = NetBarList;
                this.drpNetBar2.DataBind();
                this.drpNetBar2.Items.Insert(0, new ListItem("所有记录","0"));
            }
        }

        protected string GetBarName(int ID)
        {
            LoadNetBarList();
            string name = "[不确定]";
            if (ID < 1 || NetBarList == null || NetBarList.Count <= 0 || NetBarList[0]==null)
            {
                return name;
            }
            foreach (NetBarInfo netbar in NetBarList)
            {
                if (netbar.ID == ID)
                    return netbar.Name;
            }
            return name;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblDeptName.Text = "充值卡";
            this.usPager.PagerChanged += new Admin.Controls.Pagination.PagerChangeHandler(usPager_PagerChanged);
            if (!this.IsPostBack)
            {
                this.drpPagerSize.Items.Clear();
                this.drpPagerSize.Items.Add(new ListItem("每页10行", "10"));
                this.drpPagerSize.Items.Add(new ListItem("每页20行", "20"));
                this.drpPagerSize.Items.Add(new ListItem("每页50行", "50"));

                this.drpStatus.Items.Add(new ListItem("所有记录", "-1"));
                this.drpStatus.Items.Add(new ListItem("停用", "0"));
                this.drpStatus.Items.Add(new ListItem("启用", "1"));
                this.drpStatus.Items.Add(new ListItem("已用", "2"));

                this.dpValueType.Items.Add(new ListItem("10", "10"));
                this.dpValueType.Items.Add(new ListItem("50", "50"));
                this.dpValueType.Items.Add(new ListItem("100", "100"));

                this.drpStatus.SelectedIndex = 0;
                this.dpValueType.SelectedIndex = 0;
                if (!string.IsNullOrEmpty(Request["pagesize"]))
                {
                    this.drpPagerSize.SelectedIndex = int.Parse(Request["pagesize"]);
                }
                else
                {
                    this.drpPagerSize.SelectedValue = "10";
                }

                if (!string.IsNullOrEmpty(Request["current"]))
                {
                    ResetList(int.Parse(Request["current"]));
                }
                else
                {
                    ResetList();
                }
                //this.lvNews.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvNews_ItemCommand);
                LoadNetBar();
            }
        }
        private void usPager_PagerChanged(object sender, int e)
        {
            Refresh();
        }
        public void drpStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        public void Generate(object sender, EventArgs e)
        {
            SaveCodes(false);
        }
        public void GenerateAndSaveTxt(object sender, EventArgs e)
        {
            SaveCodes(true);
        }

        public void SaveCodes(bool isReport)
        {
            if (string.IsNullOrEmpty(tbValueType.Text) || string.IsNullOrEmpty(tbValueType.Text.Trim()))
            {
                ShowCurError("必须填写生成数目");
                return;
            }
            if (string.IsNullOrEmpty(tbNum.Text) || string.IsNullOrEmpty(tbNum.Text.Trim()) || tbNum.Text.Trim().Length!=3)
            {
                ShowCurError("必须填写卡号前“三位”");
                return;
            }
            try
            {
                int.Parse(tbNum.Text.Trim());
            }
            catch
            {
                ShowCurError("卡号前三位必须填数字");
                return;
            }
            int value = int.Parse(this.dpValueType.Text);
            int k = 0;
            try
            {
                k = int.Parse(tbValueType.Text.Trim());
            }
            catch
            {
                ShowCurError("生成数目必须填正整数");
                return;
            }
            if (k <= 0)
            {
                ShowCurError("生成数目必须填正整数");
                return;
            }
            RechargeCodeRecord code = new RechargeCodeRecord();
            RechargeCodeBO giftorder = new RechargeCodeBO();
            StringBuilder builder = new StringBuilder();
            string temp = "";
            for (int index = 0; index < k; index++)
            {
                temp = GenerateChargeCode.Generate(value, tbNum.Text.Trim());
                code.RechargeCode = CommonEncrypt.Encode(temp);
                code.RechargeValue = value;
                code.Status = 0;
                code.NetBarId = int.Parse(dpNetBar.SelectedValue);
                code.CreateTime = DateTime.Now;
                code.UsedTime = code.CreateTime;
                giftorder.Add(code);
                if (isReport)
                {
                    builder.AppendLine(temp);
                  
                }
            }
            if (isReport)
            {
                Response.Clear();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("content-disposition", "attachment;filename=Codes.txt;");
                Response.Write(builder.ToString());
                Response.End();
            }
            else
            {
                Response.Write("<script>alert('生成成功');</script>");
            }
        }

        public void ListUpdate(object sender, EventArgs e)
        {
            ResetList();
        }

        public void Refresh()
        {
            RechargeCodeBO giftorder = new RechargeCodeBO();
            lvNews.DataSource = giftorder.GetList(this.usPager.CurrentPageIndex, int.Parse(this.drpPagerSize.SelectedValue), int.Parse(this.drpStatus.SelectedValue), int.Parse(this.drpNetBar2.SelectedValue));
            lvNews.DataBind();
        }

        protected void drpPagerSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        public void ResetList()
        {
            ResetList(1);
        }

        public void SearchValue(object sender, EventArgs e)
        {
            ResetList();
        }

        public void ResetList(int current)
        {
            RechargeCodeBO giftorder = new RechargeCodeBO();
            this.usPager.RecordCount = giftorder.GetTotalSize(int.Parse( this.drpStatus.SelectedValue ),int.Parse(this.drpNetBar2.SelectedValue));
            this.usPager.PageSize = int.Parse(this.drpPagerSize.SelectedValue);
            this.usPager.Initial();
            this.usPager.CurrentPageIndex = current;
        }

        protected void drpNetBar_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        protected IList<int> checkids = new List<int>();
        protected void CreateCheckids(IList<int> checkids)
        {
            if(checkids==null)
                checkids = new List<int>();
            if (checkids.Count>0)
                checkids.Clear();
            CheckBox checkbox = null;
            foreach (ListViewItem item in lvNews.Items)
            {
                foreach (Control c in item.Controls)
                {
                    if (c is CheckBox && c.ID == "cbCard")
                    {
                        checkbox = c as CheckBox;
                        if (checkbox.Checked)
                        {
                            checkids.Add(int.Parse(checkbox.CssClass));
                        }
                        break;
                    }
                }
            }
            
        }
        protected void bt_Click(object sender, EventArgs e)
        {
            CreateCheckids(checkids);
            if (checkids == null || checkids.Count <= 0)
            {
                ShowCurError("至少要选择一张卡");
                return;
            }
            RechargeCodeBO giftorder = new RechargeCodeBO();
            if (giftorder.Valid(checkids))
            {
                ShowCurError("批量生效卡成功");
                ResetList(this.usPager.CurrentPageIndex);
            }
            else
            {
                ShowCurError("批量生效卡失败");
            }
        }


        protected void cbCard_Load(object sender, EventArgs e)
        {
            //(sender as CheckBox).Checked = false;
        }
    }
}